package com.doudou.commons.config;

import com.doudou.commons.entity.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.MissingServletRequestParameterException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartException;
import org.springframework.web.multipart.support.MissingServletRequestPartException;

import javax.validation.UnexpectedTypeException;
import java.sql.SQLSyntaxErrorException;

/**
 * @Author: 傻男人
 * @Date: 2020/5/22 11:40
 * @Version: 1.0
 * @Description: 全局异常处理
 */
@ControllerAdvice
@Slf4j
public class GlobalExceptionHandler {

    @ExceptionHandler(Exception.class)
    @ResponseBody
    public Result exceptionHandler(Exception e) {
        return Result.error("内部服务出错了",e.getMessage());
    }

    @ExceptionHandler(RuntimeException.class)
    @ResponseBody
    public Result exceptionHandler(RuntimeException e) {
        return Result.error(e.getMessage(),e.getLocalizedMessage());
    }

    @ExceptionHandler(SQLSyntaxErrorException.class)
    @ResponseBody
    public Result sQLSyntaxErrorExceptionHandler(SQLSyntaxErrorException e) {
        return Result.error(1000,"数据库出错了",e);
    }


    @ExceptionHandler(MultipartException.class)
    @ResponseBody
    public Result multipartExceptionHandler(MultipartException e) {
        return Result.error(1000,"请求中缺少Multipart",e);
    }

    @ExceptionHandler(MissingServletRequestPartException.class)
    @ResponseBody
    public Result missingServletRequestPartExceptionExceptionHandler(MissingServletRequestPartException e) {
        return Result.error(2000,"缺少"+e.getRequestPartName()+"参数",e);
    }

    @ExceptionHandler(MissingServletRequestParameterException.class)
    @ResponseBody
    public Result missingServletRequestParameterExceptionHandler(MissingServletRequestParameterException e) {
        return Result.error(2000,"缺少"+e.getParameterName()+"参数",e);
    }

    @ExceptionHandler(HttpRequestMethodNotSupportedException.class)
    @ResponseBody
    public Result httpRequestMethodNotSupportedExceptionHandler(HttpRequestMethodNotSupportedException e) {
        return Result.error(505,"不支持当前请求"+e.getMethod(),e);
    }

    @ExceptionHandler(IllegalArgumentException.class)
    @ResponseBody
    public Result illegalArgumentExceptionHandler(IllegalArgumentException e) {
        return Result.error(606,"参数非法,请检查参数格式",e);
    }

    @ExceptionHandler(UnexpectedTypeException.class)
    @ResponseBody
    public Result unexpectedTypeExceptionExceptionHandler(UnexpectedTypeException e) {
        return Result.error(607,"参数类型转换异常",e);
    }

    @ExceptionHandler(ClassCastException.class)
    @ResponseBody
    public Result classCastExceptionExceptionHandler(ClassCastException e) {
        return Result.error(601,"类型转换异常",e);
    }


}